package designpattern.observer;

import java.util.ArrayList;
import java.util.List;

public class ConcreteSubject<E> extends Subject<E> {

    private final List<Observer<E>> observers = new ArrayList<>();

    @Override
    public boolean attach(Observer<E> observer) {
        if (this.observers.contains(observer)) return false;
        this.observers.add(observer);
        return true;
    }

    @Override
    public boolean detach(Observer<E> observer) {
        return this.observers.remove(observer);
    }

    @Override
    public void notify(E event) {
        observers.forEach(observer -> {
            observer.update(event);
        });
    }
}
